package pb

import (
	"packet"
)

// GoodsList 物品列表
type GoodsList struct {
	GoodsItems               []*GoodsItem //物品列表
}

func (this *GoodsList) Encode() []byte {
	pack := this.encode()
	return pack.Encode(uint16(2020))
}

func (this *GoodsList) EncodeMsg() []byte {
	pack := this.encode()
	return pack.ReadBytes()
}

func GoodsListDecode(pack *packet.Packet) *GoodsList {
	goodsList := &GoodsList{}

	GoodsItemsCount := pack.ReadUint16()
	for ;GoodsItemsCount > 0; GoodsItemsCount-- {
		goodsList.GoodsItems = append(goodsList.GoodsItems, GoodsItemDecode(pack))
	}

	return goodsList
}

func (this *GoodsList) encode() *packet.Packet {
	pack := packet.NewWriteBuff(64)

	GoodsItemsCount := uint16(len(this.GoodsItems))
	pack.WriteUint16(GoodsItemsCount)
	for i := uint16(0); i < GoodsItemsCount; i++ {
		pack.WriteBytes(this.GoodsItems[i].EncodeMsg())
	}

	return pack
}
